package com.zcx.qshop.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonShopcartAsyGet;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public class CalculatorView extends LinearLayout implements View.OnClickListener{

    @BoundView(R.id.view_calculator_add) private View add;

    @BoundView(R.id.view_calculator_count) private TextView count;

    @BoundView(R.id.view_calculator_subtract) private View subtract;

    private OnCountChangeListener onCountChangeListener;

    private JsonShopcartAsyGet.Info.Shop shop;

    public CalculatorView(Context context, AttributeSet attrs) {

        super(context, attrs);

        BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(context).inflate(R.layout.view_calculator, this)));

        add.setOnClickListener(this);

        subtract.setOnClickListener(this);

    }

    public void setCount(String sum){

        count.setText(sum);

    }

    public int getCount(){

        return Integer.valueOf(count.getText().toString());

    }

    public void setClickable(boolean isEnabled){

        add.setClickable(isEnabled);

        subtract.setClickable(isEnabled);

    }

    public void setOnCountChangeListener(JsonShopcartAsyGet.Info.Shop shop, OnCountChangeListener onCountChangeListener){

        this.shop = shop;

        this.onCountChangeListener = onCountChangeListener;

    }

    @Override
    public void onClick(View v) {

        int count = 0;

        switch (v.getId()){

            case R.id.view_calculator_add:

                count = getCount() + 1;

                break;

            case R.id.view_calculator_subtract:

                count = getCount() > 1 ? getCount() -1 : 1;

                break;

        }

        if(onCountChangeListener != null){

            onCountChangeListener.onCountChange(shop, this, count);

        }

    }

    public interface OnCountChangeListener{

        void onCountChange(JsonShopcartAsyGet.Info.Shop shop, CalculatorView calculatorView, int count);

    }

}
